<!doctype html><html lang dir=ltr><meta charset=utf-8><meta name=viewport content="width=device-width,initial-scale=1"><title>K3s 站群架构设计 | Hui.Ke - Blog</title><meta name=generator content="Hugo Eureka 0.9.3"><link rel=stylesheet href=https://b.hui.ke/css/eureka.min.9cec6350e37e534b0338fa9a085bf06855de3b0f2dcf857e792e5e97b07ea905d4d5513db554cbc26a9c3da622bae92d.css><script defer src=https://b.hui.ke/js/eureka.min.fa9a6bf6d7a50bb635b4cca7d2ba5cf3dfb095ae3798773f1328f7950028b48c17d06276594e1b5f244a25a6c969a705.js></script>
<link rel=preconnect href=https://fonts.googleapis.com><link rel=preconnect href=https://fonts.gstatic.com crossorigin><link rel=preload href="https://fonts.googleapis.com/css2?family=Lora:wght@400;600;700&family=Noto+Serif+SC:wght@400;600;700&display=swap" as=style onload='this.onload=null,this.rel="stylesheet"'><link rel=stylesheet href=https://cdn.jsdelivr.net/gh/highlightjs/cdn-release@11.4.0/build/styles/vs.min.css media=print onload='this.media="all",this.onload=null' crossorigin><script defer src=https://cdn.jsdelivr.net/gh/highlightjs/cdn-release@11.4.0/build/highlight.min.js crossorigin></script>
<script defer src=https://cdn.jsdelivr.net/gh/highlightjs/cdn-release@11.4.0/build/languages/bash.min.js crossorigin></script>
<script defer src=https://cdn.jsdelivr.net/gh/highlightjs/cdn-release@11.4.0/build/languages/ini.min.js crossorigin></script>
<script defer src=https://cdn.jsdelivr.net/gh/highlightjs/cdn-release@11.4.0/build/languages/json.min.js crossorigin></script>
<script defer src=https://cdn.jsdelivr.net/gh/highlightjs/cdn-release@11.4.0/build/languages/php.min.js crossorigin></script>
<script defer src=https://cdn.jsdelivr.net/gh/highlightjs/cdn-release@11.4.0/build/languages/python.min.js crossorigin></script>
<script defer src=https://cdn.jsdelivr.net/gh/highlightjs/cdn-release@11.4.0/build/languages/shell.min.js crossorigin></script>
<script defer src=https://cdn.jsdelivr.net/gh/highlightjs/cdn-release@11.4.0/build/languages/sql.min.js crossorigin></script>
<script defer src=https://cdn.jsdelivr.net/gh/highlightjs/cdn-release@11.4.0/build/languages/x86asm.min.js crossorigin></script>
<script defer src=https://cdn.jsdelivr.net/gh/highlightjs/cdn-release@11.4.0/build/languages/xml.min.js crossorigin></script>
<link rel=stylesheet href=https://b.hui.ke/css/highlightjs.min.2958991528e43eb6fc9b8c4f2b8e052f79c4010718e1d1e888a777620e9ee63021c2c57ec7417a3108019bb8c41943e6.css media=print onload='this.media="all",this.onload=null'><script defer type=text/javascript src=https://lib.baomitu.com/font-awesome/6.1.1/js/all.min.js></script>
<link rel=stylesheet href=https://cdn.jsdelivr.net/npm/katex@0.15.2/dist/katex.min.css integrity=sha384-MlJdn/WNKDGXveldHDdyRP1R4CTHr3FeuDNfhsLPYrq2t0UBkUdK2jyTnXPEK1NQ media=print onload='this.media="all",this.onload=null' crossorigin><script defer src=https://cdn.jsdelivr.net/npm/katex@0.15.2/dist/katex.min.js integrity=sha384-VQ8d8WVFw0yHhCk5E8I86oOhv48xLpnDZx5T9GogA/Y84DcCKWXDmSDfn13bzFZY crossorigin></script>
<script defer src=https://cdn.jsdelivr.net/npm/katex@0.15.2/dist/contrib/auto-render.min.js integrity=sha384-+XBljXPPiv+OzfbB3cVmLHf4hdUFHlWNZN5spNQ7rmHTXpd7WvJum6fIACpNNfIR crossorigin></script>
<script>document.addEventListener("DOMContentLoaded",function(){renderMathInElement(document.body,{delimiters:[{left:"$$",right:"$$",display:!0},{left:"$",right:"$",display:!1},{left:"\\(",right:"\\)",display:!1},{left:"\\[",right:"\\]",display:!0}]})})</script><script defer src=https://cdn.jsdelivr.net/npm/mermaid@8.14.0/dist/mermaid.min.js integrity=sha384-atOyb0FxAgN9LyAc6PEf9BjgwLISyansgdH8/VXQH8p2o5vfrRgmGIJ2Sg22L0A0 crossorigin></script>
<link rel=icon type=image/png sizes=32x32 href=https://b.hui.ke/icon_hub7ca0b5404c6d576559b2bd22c64b0e5_2009_32x32_fill_box_center_3.png><link rel=apple-touch-icon sizes=180x180 href=https://b.hui.ke/icon_hub7ca0b5404c6d576559b2bd22c64b0e5_2009_180x180_fill_box_center_3.png><meta name=description content="  K3s 和面板刚装完，挂在免费空间的导航网就挂了……所以现在不得不先研究如何用 K3s 搭建站群了。"><script type=application/ld+json>{"@context":"https://schema.org","@type":"BreadcrumbList","itemListElement":[{"@type":"ListItem","position":1,"name":"Posts","item":"https://b.hui.ke/posts/"},{"@type":"ListItem","position":2,"name":"K3s 站群架构设计","item":"https://b.hui.ke/posts/architecture-design-of-station-group-on-k3s/"}]}</script><script type=application/ld+json>{"@context":"https://schema.org","@type":"Article","mainEntityOfPage":{"@type":"WebPage","@id":"https://b.hui.ke/posts/architecture-design-of-station-group-on-k3s/"},"headline":"K3s 站群架构设计 | Hui.Ke - Blog","datePublished":"2022-05-05T13:31:21+08:00","dateModified":"2022-05-08T11:33:21+08:00","wordCount":1424,"author":{"@type":"Person","name":"Hui.Ke"},"publisher":{"@type":"Person","name":"Hui.Ke","logo":{"@type":"ImageObject","url":"https://b.hui.ke/icon.png"}},"description":"\u003cp\u003e  K3s 和面板刚装完，挂在免费空间的导航网就挂了……所以现在不得不先研究如何用 K3s 搭建站群了。\u003c\/p\u003e"}</script><meta property="og:title" content="K3s 站群架构设计 | Hui.Ke - Blog"><meta property="og:type" content="article"><meta property="og:image" content="https://b.hui.ke/icon.png"><meta property="og:url" content="https://b.hui.ke/posts/architecture-design-of-station-group-on-k3s/"><meta property="og:description" content="  K3s 和面板刚装完，挂在免费空间的导航网就挂了……所以现在不得不先研究如何用 K3s 搭建站群了。"><meta property="og:site_name" content="Hui.Ke - Blog"><meta property="article:published_time" content="2022-05-05T13:31:21+08:00"><meta property="article:modified_time" content="2022-05-08T11:33:21+08:00"><meta property="article:section" content="posts"><meta property="article:tag" content="站群"><meta property="article:tag" content="架构"><meta property="og:see_also" content="https://b.hui.ke/posts/kubectl-and-k3d/"><meta property="og:see_also" content="https://b.hui.ke/posts/k3s-dashboards/"><meta property="og:see_also" content="https://b.hui.ke/posts/install-k3s/"><script>!function(e){"use strict";!function(){var i,s=window,o=document,a=e,c="".concat("https:"===o.location.protocol?"https://":"http://","sdk.51.la/js-sdk-pro.min.js"),n=o.createElement("script"),r=o.getElementsByTagName("script")[0];n.type="text/javascript",n.setAttribute("charset","UTF-8"),n.async=!0,n.src=c,n.id="LA_COLLECT",a.d=n,i=function(){s.LA.ids.push(a)},s.LA?s.LA.ids&&i():(s.LA=e,s.LA.ids=[],i()),r.parentNode.insertBefore(n,r)}()}({id:"Jgb8aUbG5e3rqhrs",ck:"Jgb8aUbG5e3rqhrs",autoTrack:!0,hashMode:!0})</script><body class="flex min-h-screen flex-col"><header class="min-h-16 pl-scrollbar bg-secondary-bg fixed z-50 flex w-full items-center shadow-sm"><div class="mx-auto w-full max-w-screen-xl"><script>let storageColorScheme=localStorage.getItem("lightDarkMode");((storageColorScheme=="Auto"||storageColorScheme==null)&&window.matchMedia("(prefers-color-scheme: dark)").matches||storageColorScheme=="Dark")&&document.getElementsByTagName("html")[0].classList.add("dark")</script><nav class="flex items-center justify-between flex-wrap px-4 py-4 md:py-0"><a href=/ class="me-6 text-primary-text text-xl font-bold">Hui.Ke - Blog</a>
<button id=navbar-btn class="md:hidden flex items-center px-3 py-2" aria-label="Open Navbar">
<i class="fas fa-bars"></i></button><div id=target class="hidden block md:flex md:grow md:justify-between md:items-center w-full md:w-auto text-primary-text z-20"><div class="md:flex md:h-16 text-sm md:grow pb-4 md:pb-0 border-b md:border-b-0"><a href=/posts/ class="block mt-4 md:inline-block md:mt-0 md:h-(16-4px) md:leading-(16-4px) box-border md:border-t-2 md:border-b-2 selected-menu-item me-4">Posts</a>
<a href=/docs/ class="block mt-4 md:inline-block md:mt-0 md:h-(16-4px) md:leading-(16-4px) box-border md:border-t-2 md:border-b-2 border-transparent me-4">Docs</a>
<a href=/categories/ class="block mt-4 md:inline-block md:mt-0 md:h-(16-4px) md:leading-(16-4px) box-border md:border-t-2 md:border-b-2 border-transparent me-4">Categories</a>
<a href=/series/ class="block mt-4 md:inline-block md:mt-0 md:h-(16-4px) md:leading-(16-4px) box-border md:border-t-2 md:border-b-2 border-transparent me-4">Series</a>
<a href=/tags/ class="block mt-4 md:inline-block md:mt-0 md:h-(16-4px) md:leading-(16-4px) box-border md:border-t-2 md:border-b-2 border-transparent me-4">Tags</a>
<a href=/love/ class="block mt-4 md:inline-block md:mt-0 md:h-(16-4px) md:leading-(16-4px) box-border md:border-t-2 md:border-b-2 border-transparent me-4">Love</a>
<a href=/about/ class="block mt-4 md:inline-block md:mt-0 md:h-(16-4px) md:leading-(16-4px) box-border md:border-t-2 md:border-b-2 border-transparent me-4">About</a></div><div class=flex><div class="relative pt-4 md:pt-0"><div class="cursor-pointer hover:text-eureka" id=lightDarkMode><i class="fas fa-adjust"></i></div><div class="fixed hidden inset-0 opacity-0 h-full w-full cursor-default z-30" id=is-open></div><div class="absolute flex flex-col start-0 md:start-auto end-auto md:end-0 hidden bg-secondary-bg w-48 rounded py-2 border border-tertiary-bg cursor-pointer z-40" id=lightDarkOptions><span class="px-4 py-1 hover:text-eureka" name=Light>Light</span>
<span class="px-4 py-1 hover:text-eureka" name=Dark>Dark</span>
<span class="px-4 py-1 hover:text-eureka" name=Auto>Auto</span></div></div></div></div><div class="fixed hidden inset-0 opacity-0 h-full w-full cursor-default z-0" id=is-open-mobile></div></nav><script>let element=document.getElementById("lightDarkMode");storageColorScheme==null||storageColorScheme=="Auto"?document.addEventListener("DOMContentLoaded",()=>{window.matchMedia("(prefers-color-scheme: dark)").addEventListener("change",switchDarkMode)}):storageColorScheme=="Light"?(element.firstElementChild.classList.remove("fa-adjust"),element.firstElementChild.setAttribute("data-icon","sun"),element.firstElementChild.classList.add("fa-sun")):storageColorScheme=="Dark"&&(element.firstElementChild.classList.remove("fa-adjust"),element.firstElementChild.setAttribute("data-icon","moon"),element.firstElementChild.classList.add("fa-moon")),document.addEventListener("DOMContentLoaded",()=>{getcolorscheme(),switchBurger()})</script></div></header><main class="grow pt-16"><div class=pl-scrollbar><div class="mx-auto w-full max-w-screen-xl lg:px-4 xl:px-8"><div class="grid grid-cols-2 gap-4 lg:grid-cols-8 lg:pt-12"><div class="bg-secondary-bg col-span-2 rounded px-6 py-8 lg:col-span-6"><article class=prose><h1 class=mb-4>K3s 站群架构设计</h1><div class="text-tertiary-text not-prose mt-2 flex flex-row flex-wrap items-center"><div class="me-6 my-2"><i class="fas fa-calendar me-1"></i>
<span>2022-05-05</span></div><div class="me-6 my-2"><i class="fa-solid fa-pen-to-square me-1"></i>
<span>2022-05-08</span></div><div class="me-6 my-2"><i class="fas fa-clock me-1"></i>
<span>3 min read</span></div><div class="me-6 my-2"><i class="fas fa-folder me-1"></i>
<a href=https://b.hui.ke/categories/it/ class=hover:text-eureka>IT</a></div><div class="me-6 my-2"><i class="fas fa-th-list me-1"></i>
<a href=https://b.hui.ke/series/k3s/ class=hover:text-eureka>K3s</a></div><div class="me-6 my-2"><i class="fa-solid fa-eye me-1"></i>
<span id=busuanzi_value_page_pv><i class="fa fa-spinner fa-spin"></i></span> Hits</div></div><p>  K3s 和面板刚装完，挂在免费空间的导航网就挂了……所以现在不得不先研究如何用 K3s 搭建站群了。</p><p>  之前搭建网站一直是用 CyberPanel 或者宝塔面板，一键安装 Nginx、PHP、Mysql 和 Memcached 等等。但现在需要换到 K3s 集群来了，这和服务器是完全不一样的“底层逻辑”，所以结合 K3s 的特性，我对站群架构的设计有如下几个方向。</p><h2 id=老套路还是使用-cyberpanel-面板>老套路：还是使用 CyberPanel 面板</h2><p>  还是使用面板，那么就很简单，只需要在 K3s 集群中开一个 Pod 装上 CyberPanel 的面板就行了，然后如何建站一切照旧。接下来只要考虑公网访问的 IP 和端口映射的问题了。</p><p>  当然这个架构还要考虑数据库持久化的问题，数据库肯定不能装在 Pod 里，否则分分钟就无了……</p><h2 id=新玩法使用-k3s-部署各种服务>新玩法：使用 K3s 部署各种服务</h2><p>  这个呢无疑是最优方案，使用 K3s 自己部署 Nginx、PHP、Mysql 等等，但这也无疑是最难学习和操作的，博主只能结合以下官方教程和 K3s 原理来逐步的深入推进</p><ol><li>部署 Nginx 可以参考 Kuboard 官方教程：<a href=https://www.kuboard.cn/learning/k8s-basics/deploy-app.html>实战：部署 nginx Deployment</a></li><li>部署 Mysql 可以参考 Kuboard 官方教程：<a href=https://www.kuboard.cn/learning/k8s-practice/ocp/mysql.html>在K8S上部署mysql</a></li><li>部署 Wordpress 可以参考 Kubernetes 官方教程：<a href=https://kubernetes.io/zh/docs/tutorials/stateful-application/mysql-wordpress-persistent-volume/>示例：使用 Persistent Volumes 部署 WordPress 和 MySQL</a></li></ol><h2 id=俄罗斯套娃在-k3s-集群中安装-cyberpanel-面板然后在面板中使用-docker-部署站群>俄罗斯套娃：在 K3s 集群中安装 CyberPanel 面板，然后在面板中使用 Docker 部署站群</h2><p>  因为不想还是用“老套路”，在短期内又玩不转“新玩法”，所以出此下策就想到了这种套娃式的架构……</p><p>  翻译一下就是</p><ol><li>首先在 K3s 集群中创建一个 Pod（可以理解为“容器”）</li><li>接着在 Pod 中安装 CyberPanel 面板</li><li>然后在 CyberPanel 面板中再使用 Docker（容器）</li><li>最后在 Docker 中再安装网站</li></ol><p>  由此就形成了一个俄罗斯套娃架构……</p><p>  直观上看，就感觉这样会产生很多无用的资源浪费，例如我们只是使用了 CyberPanel 的 Docker 功能，但却需要安装整个面板。</p><p>  还有就是这样完全违背了 K3s 的用途，在“容器”中安装“容器”，效率肯定低下不说，K3s 本身就是容器为什么还要套容器呢？</p><p>  当然这也有一个好处且提供了一个新的思路，那就是：可以将静态网站和安装完成运行后不会产生新数据的动态网站（例如导航站）打包成 Docker 镜像，往后均以 Docker 的方式部署（其实博主早有此想法，只是一直没有很刚需就懒得弄了……）。</p><p>  由此一来，往后网站服务器再出问题，或者需要迁移网站时，只需要拉取 Docker 镜像再部署就完事了，不用每次再配置 Wordpress 等等（没错博主的导航站就是 WP 的），可以省去大量配置时间并且也降低了出错的可能性，而且万一有什么问题（例如被渗透了）也可以随时删除容器重建。</p><blockquote><p>此种方式也需考虑数据库持久化的问题，原因同第一个架构。</p></blockquote><h2 id=俄罗斯套娃引申出的非套娃版每个网站部署一个-pod>俄罗斯套娃引申出的“非套娃版”：每个网站部署一个 Pod</h2><p>  这个架构呢由上述架构的思路而来，意思就是每个网站都部署各自的 Pod，互不干扰。简单的将整个 K3S 当作一个 Docker 管理器，然后为每个网站创建一个 Docker，这样做呢也是利用了上一个架构的思路，即将网站制成 Docker。但是很明显存在几个问题</p><ol><li>首先也是资源的浪费，假如有多个动态网站，无法共用同一个 PHP，每一个 Pod 中都需要安装 PHP</li><li>其次是违背了 K3s 的初心，如此使用和安装 Docker 管理器没有太大差异了，也就是差一个 Docker 不支持 Agent 的区别了</li><li>还有一个技术问题，因为前端没有 Nginx 作网站的“分流”，所有网站都共用一个 443 https 端口，所以得要给不同的 Pod 设置不同的公网 IP（这个能否实现博主还未考证）</li></ol><p>  但是这个架构可以快速的搭建起站群，弱化 K3s 的深层原理和运行机制，将 K3s 与 Docker 的优势相结合，既能便捷的拉取 Docker 镜像部署，又能利用 K3s 的节点调度资源，能在初学 K3s 还不清楚其机制时，完成快速部署并满足一定程度上的“高可用”。</p><p>  但是由于之前提到的几个问题，这个架构可能也只能算是一个初学者的临时解决方案，并不适合深入研究和实现。</p><blockquote><p>此种方式也需考虑数据库持久化的问题，原因同第一个架构。</p></blockquote></article><div class=my-4><a href=https://b.hui.ke/tags/%E7%AB%99%E7%BE%A4/ class="inline-block bg-tertiary-bg text-sm rounded px-3 py-1 my-1 me-2 hover:text-eureka">#站群</a>
<a href=https://b.hui.ke/tags/%E6%9E%B6%E6%9E%84/ class="inline-block bg-tertiary-bg text-sm rounded px-3 py-1 my-1 me-2 hover:text-eureka">#架构</a></div><div class=py-2><div class="my-8 flex flex-col items-center md:flex-row"><a href=https://b.hui.ke/authors/hui.ke/ class="md:me-4 text-primary-text h-24 w-24"><img src=https://b.hui.ke/bagua.webp class="bg-primary-bg w-full rounded-full" alt=Avatar></a><div class="mt-4 w-full md:mt-0 md:w-auto"><a href=https://b.hui.ke/authors/hui.ke/ class="mb-2 block border-b pb-1 text-lg font-bold"><h3>Hui.Ke</h3></a><span class="block pb-2">❤ Cyber Security | Safety is a priority.</span>
<a href=mailto:3199731997@qq.com class=me-2><i class="fas fa-envelope"></i></a>
<a href="https://wpa.qq.com/msgrd?v=3&uin=3199731997" class=me-2><i class="fab fa-qq"></i></a>
<a href=/images/aixinxianquan.webp class=me-2><i class="fab fa-weixin"></i></a></div></div></div><div class="-mx-2 mt-4 flex flex-col border-t px-2 pt-4 md:flex-row md:justify-between"><div><span class="text-primary-text block font-bold">Previous</span>
<a href=https://b.hui.ke/posts/k3s-dashboards/ class=block>K3s Dashboards</a></div><div class="mt-4 md:mt-0 md:text-right"><span class="text-primary-text block font-bold">Next</span>
<a href=https://b.hui.ke/posts/install-k3s/ class=block>安装 K3s</a></div></div><div id=valine-comments class=mt-4></div><script defer src=https://cdn.jsdelivr.net/npm/valine@1.4.16/dist/Valine.min.js integrity=sha384-e0+DNUCJo75aOAzHQbFWYBCM9/S4f0BhRJXvEgbE3mMS85RM20MSSGStHuNdY2QK crossorigin></script>
<script>document.addEventListener("DOMContentLoaded",function(){new Valine({el:"#valine-comments",appId:"BQnVqWIiq78AdqwyhvBVAa3y-MdYXbMMI",appKey:"RKg5By312YjM8rU6WkkfK9IN",recordIP:"true",serverURLs:"https://l.hui.ke",visitor:"true"})})</script></div><div class=col-span-2><div class="bg-secondary-bg prose max-w-none rounded p-6"><h3>Series of Posts</h3><a href=https://b.hui.ke/posts/kubectl-and-k3d/ class=no-underline>Kubectl & K3d</a><br><a href=https://b.hui.ke/posts/k3s-dashboards/ class=no-underline>K3s Dashboards</a><br><a href=https://b.hui.ke/posts/architecture-design-of-station-group-on-k3s/ class=no-underline>K3s 站群架构设计</a><br><a href=https://b.hui.ke/posts/install-k3s/ class=no-underline>安装 K3s</a><br></div><div class="bg-primary-bg
prose sticky top-16 z-10 hidden px-6 py-4 lg:block"><h3>On This Page</h3></div><div class="sticky-toc hidden px-6 pb-6 lg:block"><nav id=TableOfContents><ul><li><a href=#老套路还是使用-cyberpanel-面板>老套路：还是使用 CyberPanel 面板</a></li><li><a href=#新玩法使用-k3s-部署各种服务>新玩法：使用 K3s 部署各种服务</a></li><li><a href=#俄罗斯套娃在-k3s-集群中安装-cyberpanel-面板然后在面板中使用-docker-部署站群>俄罗斯套娃：在 K3s 集群中安装 CyberPanel 面板，然后在面板中使用 Docker 部署站群</a></li><li><a href=#俄罗斯套娃引申出的非套娃版每个网站部署一个-pod>俄罗斯套娃引申出的“非套娃版”：每个网站部署一个 Pod</a></li></ul></nav></div><script>window.addEventListener("DOMContentLoaded",()=>{enableStickyToc()})</script></div></div><script>document.addEventListener("DOMContentLoaded",()=>{hljs.highlightAll()})</script></div></div></main><footer class=pl-scrollbar><div class="mx-auto w-full max-w-screen-xl"><div class="text-center p-6 pin-b"><script async src=/js/click.js></script><div id=poem_ip></div><script type=text/javascript>jinrishici.load(function(e){tags.innerHTML=e.data.matchTags})</script><div><span id=timeDate>载入年天数...</span><span id=times>载入时分秒...</span>
<script async src=/js/duration.js></script></div><a href=https://www.foreverblog.cn/go.html target=_blank><img src=https://img.foreverblog.cn/wormhole_4_tp.gif alt style=display:inline-block;width:auto;height:32px title=穿梭虫洞-随机访问十年之约友链博客></a><p class="text-sm text-tertiary-text"><script async src=//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js></script>本站总访问量 <span id=busuanzi_value_site_pv></span> 次
&#183; 您是本站的第 <span id=busuanzi_value_site_uv></span> 个小伙伴</p><script async src=/js/tab.js></script></div></div></footer></body></html>